package com.yisen.dasan.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yisen.dasan.dao.FormMapper;
import com.yisen.dasan.pojo.Form;
import com.yisen.dasan.service.FormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @AuthorList: LiuYiSen
 * @Date: 2020/10/30 14:55
 */
@Service
public class FormServiceImpl implements FormService {
    @Autowired
    FormMapper formMapper;

    @Override
    public Form getForm(int id) {
        Form form = formMapper.getForm(id);
        return form;
    }
    @Override
    public boolean delForm(int formId){
        formMapper.delForm(formId);
        return true;
    }
    @Override
    public List<Map> getItem(int formId,String type){
        List<Map> list=formMapper.getItem(formId,type);
        return list;
    }
    @Override
    public Map getState(int formId){
        Map state=formMapper.getState(formId);
        return state;
    }
    @Override
    public boolean updateForm(JSONObject jsonObject){
        int formId=jsonObject.getInteger("formId");
        int state=jsonObject.getInteger("state");
        formMapper.update(formId,state);
        return true;
    }


    @Override
    public boolean updateFormNews(JSONObject jsonObject){
        if(jsonObject==null){
            return false;
        }
        System.out.println(jsonObject);
        JSONObject json=new JSONObject();
        json.put("id",jsonObject.getInteger("id"));
        json.put("formName",jsonObject.getString("formName"));
        json.put("formDes",jsonObject.getString("formDes"));
        Map map=json;
        if(formMapper.updateForm(map)==true)
            updateFormItem(jsonObject.getJSONArray("formItem"));
        return true;
    }
    public boolean updateFormItem(JSONArray jsonArray){
        for (int i=0;i<jsonArray.size();i++){
            JSONObject total=jsonArray.getJSONObject(i);
            JSONObject json=new JSONObject();
            json.put("id",total.getInteger("id"));
            json.put("title",total.getString("title"));
            json.put("tips",total.getString("tips"));
            json.put("tipsColor",total.getString("tipsColor"));
            Map map=json;
            if(formMapper.updateFormItem(map)==true)
                updateFormAny(total.getJSONArray("option"),total.getString("type"));
        }
        return true;
    }
    public boolean updateFormAny(JSONArray jsonArray,String type){
        for (int i=0;i<jsonArray.size();i++){
            JSONObject total=jsonArray.getJSONObject(i);
            if (type.equals("radio")){
                JSONObject json=new JSONObject();
                json.put("id",total.getInteger("id"));
                json.put("text",total.getString("text"));
                Map map=json;
                formMapper.updateFormRadio(map);
            }else if (type.equals("select")){
                JSONObject json=new JSONObject();
                json.put("id",total.getInteger("id"));
                json.put("text",total.getString("text"));
                json.put("default_value",total.getString("default_value"));
                Map map=json;
                formMapper.updateFormSelect(map);
            }else if (type.equals("age")){
                JSONObject json=new JSONObject();
                json.put("id",total.getInteger("id"));
                json.put("text",total.getString("text"));
                Map map=json;
                formMapper.updateFormAge(map);
            }else if (type.equals("education")){
                JSONObject json=new JSONObject();
                json.put("id",total.getInteger("id"));
                json.put("text",total.getString("text"));
                Map map=json;
                formMapper.updateFormEducation(map);
            }else if (type.equals("gender")){
                JSONObject json=new JSONObject();
                json.put("id",total.getInteger("id"));
                json.put("text",total.getString("text"));
                Map map=json;
                formMapper.updateFormGender(map);
            }else if (type.equals("multiple")){
                JSONObject json=new JSONObject();
                json.put("id",total.getInteger("id"));
                json.put("text",total.getString("text"));
                json.put("min",total.getInteger("min"));
                json.put("max",total.getInteger("max"));
                Map map=json;
                formMapper.updateFormMultiple(map);
            }else if (type.equals("birthday")){
                JSONObject json=new JSONObject();
                json.put("id",total.getInteger("id"));
                json.put("placeholder",total.getString("placeholder"));
                json.put("min",total.getInteger("min"));
                json.put("max",total.getInteger("max"));
                Map map=json;
                formMapper.updateFormBirthday(map);
            }else if (type.equals("completion")){
                JSONObject json=new JSONObject();
                json.put("id",total.getInteger("id"));
                json.put("placeholder",total.getString("placeholder"));
                json.put("min",total.getInteger("min"));
                json.put("max",total.getInteger("max"));
                Map map=json;
                formMapper.updateFormCompletion(map);
            }else if (type.equals("email")){
                JSONObject json=new JSONObject();
                json.put("id",total.getInteger("id"));
                json.put("placeholder",total.getString("placeholder"));
                json.put("min",total.getInteger("min"));
                json.put("max",total.getInteger("max"));
                Map map=json;
                formMapper.updateFormEmail(map);
            }else if (type.equals("field")){
                JSONObject json=new JSONObject();
                json.put("id",total.getInteger("id"));
                json.put("placeholder",total.getString("placeholder"));
                json.put("min",total.getInteger("min"));
                json.put("max",total.getInteger("max"));
                Map map=json;
                formMapper.updateFormField(map);
            }else if (type.equals("iphone")){
                JSONObject json=new JSONObject();
                json.put("id",total.getInteger("id"));
                json.put("placeholder",total.getString("placeholder"));
                json.put("min",total.getInteger("min"));
                json.put("max",total.getInteger("max"));
                Map map=json;
                formMapper.updateFormIphone(map);
            }else if (type.equals("mobile")){
                JSONObject json=new JSONObject();
                json.put("id",total.getInteger("id"));
                json.put("placeholder",total.getString("placeholder"));
                json.put("min",total.getInteger("min"));
                json.put("max",total.getInteger("max"));
                Map map=json;
                formMapper.updateFormMobile(map);
            }else if (type.equals("name")){
                JSONObject json=new JSONObject();
                json.put("id",total.getInteger("id"));
                json.put("placeholder",total.getString("placeholder"));
                json.put("min",total.getInteger("min"));
                json.put("max",total.getInteger("max"));
                Map map=json;
                formMapper.updateFormName(map);
            }else if (type.equals("number")){
                JSONObject json=new JSONObject();
                json.put("id",total.getInteger("id"));
                json.put("placeholder",total.getString("placeholder"));
                json.put("min",total.getInteger("min"));
                json.put("max",total.getInteger("max"));
                Map map=json;
                formMapper.updateFormNumber(map);
            }else if (type.equals("imgradio")){
                JSONObject json=new JSONObject();
                json.put("id",total.getInteger("id"));
                json.put("imageurl",total.getString("imageurl"));
                Map map=json;
                formMapper.updateFormImg(map);
            }
        }
        return true;
    }
}
